package it.trick.admin.service;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import it.trick.admin.App;
import it.trick.admin.Constants;
import it.trick.admin.Util;
import it.trick.admin.model.ChannelModel;
import java.net.URL;

public class ChannelService {

    public static List<ChannelModel> getList() throws Exception {

        String json = Http.get(App.getInstance().getSite() + Constants.GET_CHANNEL_LIST_PATH);
        Type collectionType = new TypeToken<Collection<ChannelModel>>() {
        }.getType();

        Gson gson = new Gson();

        List<ChannelModel> modelList = gson.fromJson(json, collectionType);

        return modelList;
    }

    /**
     * 追加
     * @param model
     * @return
     * @throws Exception
     */
    public static ChannelModel addChannel(ChannelModel model) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.ADD_CHANNEL_PATH);
        Type responseModelType = new TypeToken<ChannelModel>() {}.getType();

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("channel=");
        psb.append(model.getKeyValueString());
        psb.append("&");
        psb.append("name=");
        psb.append(model.getName());
        psb.append("&");
        psb.append("index=");
        psb.append(model.getIndex());

        return (ChannelModel)Http.postGetResponseJson(postURL, psb.toString(), responseModelType);
    }

    /**
     * チャンネルの取得
     * @return
     * @throws Exception
     */
    public static ChannelModel getChannel(String keyString) throws Exception {

        String json = Http.get(App.getInstance().getSite() + Constants.GET_CHANNEL_PATH + keyString);
        Type type = new TypeToken<ChannelModel>() {}.getType();

        Gson gson = new Gson();

//        ChannelModel model = gson.fromJson(json, type);
        
        ChannelModel model = (ChannelModel)Http.postGetResponseJson(new URL(App.getInstance().getSite() + Constants.GET_CHANNEL_PATH + keyString), "", type);

        return model;
    }

    /**
     * 更新
     * @param chapterModel
     * @throws Exception
     */
    public static void updateChannel (
            ChannelModel model) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.UPDATE_CHANNEL_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("channel=");
        psb.append(model.getKeyValueString());
        psb.append("&");
        psb.append("name=");
        psb.append(model.getName());
        psb.append("&");
        psb.append("index=");
        psb.append(model.getIndex());
        psb.append("&");
        psb.append("content=");
        psb.append(model.getContent());
        psb.append("&");
        psb.append("keyWord=");
        psb.append(Util.keywordListToString(model.getKeyWordList()));
        psb.append("&");
        psb.append("description=");
        psb.append(model.getDescription());

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 削除
     * @param categoryKeyString
     */
    public static void deleteChannel(String KeyString) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.DELETE_CHANNEL_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("channel=");
        psb.append(KeyString);

        Http.postOnle(postURL, psb.toString());
    }
}
